<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item name="searchKey" label="名称关键词">
								<a-input v-model:value="searchFormState.searchKey" placeholder="请输入职位名称关键词" />
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-button type="primary" @click="tableRef.refresh(true)">
								<template #icon><SearchOutlined /></template>
								查询
							</a-button>
							<a-button class="snowy-buttom-left" @click="reset">
								<template #icon><redo-outlined /></template>
								重置
							</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:expand-row-by-click="true"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<!-- <xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchPosition" /> -->
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'status'">
							<a-switch :loading="statusLoading" :checked="record.status === 'Y'" @change="editStatus(record)" />
						</template>
						<template v-if="column.dataIndex === 'defaultFlag'">
							<a-switch :loading="defLoading" :checked="record.defaultFlag === 'Y'" @change="editDefaultFlag(record)" />
						</template>
						<template v-if="column.dataIndex === 'action' && record.id >1">
							<a @click="formRef.onOpen(record)">编辑</a>
							<a-divider type="vertical" />
							<a-popconfirm title="确定删除此职位？" @confirm="removePost(record)">
								<a-button type="link" danger size="small">删除</a-button>
							</a-popconfirm>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<PostForm ref="formRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="sysPosition">
import { Empty } from 'ant-design-vue'
import { isEmpty } from 'lodash-es'
import PostApi from '@/api/authority/PostApi'
import DeptApi from '@/api/authority/DeptApi'
import PostForm from './PostForm.vue'

const columns = [
	{
		title: '职位名称',
		dataIndex: 'name'
	},
	{
		title: '状态',
		dataIndex: 'status',
		width: 100
	},
	{
		title: '是否默认',
		dataIndex: 'defaultFlag',
		width: 100
	},
	{
		title: '排序',
		dataIndex: 'sort',
		width: 100
	},
	{
		title: '备注',
		dataIndex: 'remark'
	},
	{
		title: '创建时间',
		dataIndex: 'createdTime'
	},
	{
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	}
]
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	alert: {
		show: false,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
// 定义tableDOM
const tableRef = ref(null)
const formRef = ref()
const searchFormRef = ref()
const searchFormState = ref({})
const cardLoading = ref(true)
const statusLoading = ref(false)
const defLoading = ref(false)
// 表格查询 返回 Promise 对象
const loadData = (parameter) => {
	return PostApi.page(Object.assign(parameter, searchFormState.value)).then((res) => {
		return res
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 修改是否默认
const editDefaultFlag = (record) => {
	defLoading.value = true
	const param = Object.assign({}, record)
	if (record.defaultFlag === 'Y') {
		param.defaultFlag = 'N'
	} else {
		param.defaultFlag = 'Y'
	}
	PostApi.setDef(param)
		.then(() => {
			tableRef.value.refresh()
		})
		.finally(() => {
			defLoading.value = false
		})
}
// 修改状态
const editStatus = (record) => {
	statusLoading.value = true
	const param = Object.assign({}, record)
	if (record.status === 'Y') {
		param.status = 'N'
	} else {
		param.status = 'Y'
	}
	PostApi.setStatus(param)
		.then(() => {
			tableRef.value.refresh()
		})
		.finally(() => {
			statusLoading.value = false
		})
}

// 删除
const removePost = (record) => {
	let params = [record.id]
	PostApi.delete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// // 批量删除
// const deleteBatchPosition = (params) => {
// 	let ids = params.map((item) => {
// 		return item.id
// 	})
// 	PostApi.delete(ids).then(() => {
// 		tableRef.value.clearRefreshSelected()
// 	})
// }
</script>

<style scoped>
.ant-form-item {
	margin-bottom: 0 !important;
}
.primaryAdd {
	margin-right: 10px;
}
.snowy-buttom-left {
	margin-left: 8px;
}
</style>
